
#include <stdio.h>
#include <string.h>
#include "my_uart.h"



#define UART_BUFF_SIZE_MAX 255
#define UART_LOOP_SIZE 10

enum MODELIST
{
    END_FLAG = 0,
    START_FLAG = 1
};


//协议定义区
/*
协议必须包含帧头，帧长
*/
uint8_t mode = START_FLAG;
uint8_t flag[2] = {0x77, 0x7f};
uint8_t frame_len = 11;//帧头检测模式时用到帧长

//*******************USART*******************************************

uint8_t aRxBuffer;
uint8_t Uart1_RxBuff[UART_BUFF_SIZE_MAX];
uint32_t Uart1_Rx_Cnt = 0;

//环形缓存队列
uint8_t Uart1_RxBuff_LOOP[UART_LOOP_SIZE][UART_BUFF_SIZE_MAX];

void UART_AddToLoop(uint8_t *data, uint8_t L)
{
    static uint8_t Loop_Cnt = 0;
    memcpy(&Uart1_RxBuff_LOOP[Loop_Cnt][1], data, L);
    Uart1_RxBuff_LOOP[Loop_Cnt][0] = L;
    //printf(">%d\n", Loop_Cnt);
    Loop_Cnt++;
    if (Loop_Cnt > UART_LOOP_SIZE - 1)
    {
        Loop_Cnt = 0;
    }
}
/*
开启串口中断
*/
void UART_IQR_INIT()
{
    HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    static uint8_t start = 0;
    UNUSED(huart);
    if (huart->Instance == USART1)
    {
        switch (mode)
        {
        case END_FLAG:
        {
            if (Uart1_Rx_Cnt >= 255) //
            {
                Uart1_Rx_Cnt = 0;
            }
            else
            {
                Uart1_RxBuff[Uart1_Rx_Cnt++] = aRxBuffer;                                                       
                if ((Uart1_RxBuff[Uart1_Rx_Cnt - 2] == flag[0]) && (Uart1_RxBuff[Uart1_Rx_Cnt - 1] == flag[1])) //结束符0x7f f7，单帧最长256
                {
                    UART_AddToLoop(Uart1_RxBuff, Uart1_Rx_Cnt - 2); //加入环形处理队列
                    Uart1_Rx_Cnt = 0;
                }
                if ((Uart1_RxBuff[Uart1_Rx_Cnt - 2] == '\r') && (Uart1_RxBuff[Uart1_Rx_Cnt - 1] == '\n'))
                {
                    UART_AddToLoop(Uart1_RxBuff, Uart1_Rx_Cnt - 2); //加入环形处理队列
                    Uart1_Rx_Cnt = 0;
                }
            }
        }
        break;
        case START_FLAG:
        {
           
            if (start == 2)
            {
                Uart1_RxBuff[Uart1_Rx_Cnt++] = aRxBuffer;

                if (Uart1_Rx_Cnt == frame_len - 2)
                {
                    UART_AddToLoop(Uart1_RxBuff, Uart1_Rx_Cnt);
                    Uart1_Rx_Cnt = 0;
                    start = 0;
                }
            }
            else if (aRxBuffer == flag[start])
            {
                start++;
            }
            else
            {
                start = 0;
            }
        }
        break;

        default:
            break;
        }
        HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
    }
}
/*
读取队列缓冲区数据
*/
uint8_t UART_GET_LOOP_DATA(URAT_LOOP_DATA *pdata)
{
    static uint8_t inx_count = 0;
    if (Uart1_RxBuff_LOOP[inx_count][0] > 0)
    {
        pdata->data = &Uart1_RxBuff_LOOP[inx_count][1];
        pdata->length = Uart1_RxBuff_LOOP[inx_count][0];
        Uart1_RxBuff_LOOP[inx_count][0] = 0;
        //printf("<%d\n", inx_count);
        inx_count++;
        if (inx_count > UART_LOOP_SIZE - 1)
        {
            inx_count = 0;
        }
        return pdata->length;
    }
    return 0;
}
